package com.hp.controller;

import com.github.pagehelper.PageInfo;
import com.hp.bean.Coupon;
import com.hp.service.CouponService;
import com.hp.util.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/coupon")
public class CouponController {
    @Resource
    private CouponService service;

    //用户手机端页面查看优惠价页面
    public String coupon(){
        return "/view/customerPhone/html/coupon.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(Coupon obj
            ,@RequestParam(value = "page",defaultValue = "1")Integer page
            ,@RequestParam(value = "limit",defaultValue = "10")Integer limit){
        List<Coupon> list=service.selectCouponList(obj);
        return JsonData.buildSuccess(new PageInfo<Coupon>(list));
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Coupon obj){
        Coupon coupon = service.selectCouponById(obj.getId());
        return JsonData.buildSuccess(coupon);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Coupon obj){
        int i = service.insertCoupon(obj);
        if (i>0){
            return JsonData.buildSuccess("添加成功");
        }else {
            return JsonData.buildError("添加失败");
        }

    }
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Coupon obj){
        int i = service.updateCoupon(obj);
        return i>0?JsonData.buildSuccess("修改成功！"):JsonData.buildError("修改失败！");
    }
    @RequestMapping("/selectListByPhone")
    @ResponseBody
    public Object selectListByPhone(HttpSession session) {
        if(session.getAttribute("phone")!=null){
            List<Coupon> list = service.selectListByPhone((String)session.getAttribute("phone"));
            return JsonData.buildSuccess(list);
        }
        return  JsonData.buildError("请登录后在查看订单信息");

    }
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Coupon obj){
   //     int i = service.delCouponById(obj.getId());//物理删除，也叫真删除
        obj.setDel("1");
        int i=service.updateCoupon(obj);
        return i>0?JsonData.buildSuccess("删除成功！"):JsonData.buildError("删除失败！");
    }
}
